package com.hc.service.impl;

import com.hc.dao.RoleAuthDao;
import com.hc.entity.Auth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.hc.entity.RoleAuth;
import com.hc.service.AuthService;
import com.hc.dao.AuthDao;
import com.hc.utils.PageBean;
import com.hc.utils.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthDao authDao;

    @Override
    public int addAuth(Auth auth){
        int res = authDao.insertAuth(auth);
        return res;
    }

    @Override
    public int addAuthSelective(Auth auth){
        int res = authDao.insertAuthSelective(auth);
        return res;
    }

	@Override
    public int batchAddAuth(List<Auth> authList){
		int res = authDao.batchInsertAuth(authList);
		return res;
	}

    @Override
    public int deleteAuthById(Integer id){
        int res = authDao.deleteAuthById(id);
        return res;
    }
    
 	@Override
	public int deleteAuthByCondition(Auth auth){
	 	int res = authDao.deleteAuthByCondition(auth);
        return res;
	}
	
	@Override
	public int batchDeleteAuthByIdList(List<Integer> idList){
		int res = authDao.batchDeleteAuthByIdList(idList);
        return res;
	}
	
    @Override
    public int updateAuth(Auth auth){
        int res = authDao.updateAuth(auth);
        return res;
    }

    @Override
    public int updateAuthSelective(Auth auth){
        int res = authDao.updateAuthSelective(auth);
        return res;
    }

    @Override
    public long getCount(){
        long res = authDao.selectCount();
        return res;
    }

    @Override
    public long getCountByCondition(Auth auth){
        long res = authDao.selectCountByCondition(auth);
        return res;
    }

    @Override
    public Auth getAuthById(Integer id){
        Auth res = authDao.selectAuthById(id);
        return res;
    }

    @Override
    public List<Auth> getAllAuth(){
        List<Auth> res = authDao.selectAllAuth();
        return res;
    }

	@Override
	public 	List<Auth> getAuthByCondition(Auth auth){
		List<Auth> res = authDao.selectAuthByCondition(auth);
		return res;
	}
	
    @Override
    public PageBean<Auth> getAuthWithPagination(PageParam pageParam){
        long count = authDao.selectCount();
        PageBean<Auth> pageBean = new PageBean<>(pageParam, (int)count);
        pageBean.setPageNum(pageParam.getPage());
        PageHelper.startPage(pageParam.getPage(),pageParam.getRows());
        List<Auth> data = authDao.selectAllAuth();
        pageBean.setRecords(data);
        return pageBean;
    }

	@Override
	public PageBean<Auth> getAuthWithPaginationByCondition(PageParam pageParam, Auth auth){
        long count = authDao.selectCountByCondition(auth);
        PageBean<Auth> pageBean = new PageBean<>(pageParam, (int)count);
        pageBean.setPageNum(pageParam.getPage());
        PageHelper.startPage(pageParam.getPage(),pageParam.getRows());
		List<Auth> data = authDao.selectAuthByCondition(auth);
		pageBean.setRecords(data);
        return pageBean;
	}

    //////////////////////////// 外键相关 /////////////////////////////////////
    @Override
    public List<Auth> getAuthByParent(Integer parent){
        List<Auth> res = authDao.selectAuthByParent(parent);
        return res;
    }

    @Autowired
    private RoleAuthDao roleAuthDao;

    @Override
    public Map<Auth,List<Auth>> getAuthByRoleId(Integer roleId) {
        Map<Auth,List<Auth>> res = new HashMap<>();

        List<Auth> authList = new ArrayList<>();

        //在tb_role_auth表中找到roleId所对应的auth_id的List
        RoleAuth roleAuth = RoleAuth.builder().roleId(roleId).build();
        List<RoleAuth> roleAuthList = roleAuthDao.selectRoleAuthByCondition(roleAuth);

        //根据auth_id找到Auth对象的List
        for (RoleAuth item : roleAuthList) {
            Auth auth = authDao.selectAuthById(item.getAuthId());
            authList.add(auth);
        }

        for(Auth auth : authList){ // 原型：购物车
            Auth key = authDao.selectAuthById(auth.getParentId());
            List<Auth> value  = res.get(key);
            if(value == null){
                value = new ArrayList<>();
            }
            value.add(auth);
            res.put(key,value); //放入Map
        }

       return res;
    }

}